﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace PlayListSet.UI.ValueConverter
{
    internal class TimeSpanValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {

                string valueString = (string)value;
                string[] ms = valueString.Split('.');
                var timeArr = ms[0].Split(':');
                TimeSpan timeSpan = new TimeSpan(System.Convert.ToInt32(timeArr[0]), System.Convert.ToInt32(timeArr[1]), System.Convert.ToInt32(timeArr[2]));

                if (ms.Length == 2)
                {
                    var m = TimeSpan.FromSeconds(System.Convert.ToDouble("0." + ms[1]));

                    timeSpan = timeSpan.Add(m);
                }

                return ValidationResult.ValidResult;
            }
            catch (Exception)
            {
                return new ValidationResult(false, $"时间格式错误，正确格式：00:01:00.100");
            }

        }
    }
}
